package com.sensetime.bi.senselink.server.rtsp.server.processor.proxy;

import com.sensetime.bi.senselink.server.constant.Constant;
import com.sensetime.bi.senselink.server.entity.MediaSource;
import com.sensetime.bi.senselink.server.entity.RtspRequestMsg;
import com.sensetime.bi.senselink.server.rtsp.server.processor.IRtspRequest;
import com.sensetime.bi.senselink.server.rtsp.session.Session;
import com.sensetime.bi.senselink.server.rtsp.session.SessionManager;
import io.netty.channel.Channel;
import io.netty.util.Attribute;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import static com.sensetime.bi.senselink.server.constant.Constant.SESSION_ID;

@Slf4j
@Service("RtspProxyProcessor")
public class RtspProxyProcessor implements IRtspRequest {
    @Override
    public void onOptionRequest(RtspRequestMsg request, Channel channel) {
        // 转发发送给真实的视频源Rtsp服务（IPC）
        sendToMediaServer(request, channel);
    }

    @Override
    public void onDescribeRequest(RtspRequestMsg request, Channel channel) {
        // 转发发送给真实的视频源Rtsp服务（IPC）
        sendToMediaServer(request, channel);
    }

    @Override
    public void onSetupRequest(RtspRequestMsg request, Channel channel) {
        // 转发发送给真实的视频源Rtsp服务（IPC）
        sendToMediaServer(request, channel);
    }

    @Override
    public void onPlayRequest(RtspRequestMsg request, Channel channel) {
        // 转发发送给真实的视频源Rtsp服务（IPC）
        sendToMediaServer(request, channel);
    }

    @Override
    public void onGetParameterRequest(RtspRequestMsg request, Channel channel) {
        // 转发发送给真实的视频源Rtsp服务（IPC）
        sendToMediaServer(request, channel);
    }

    @Override
    public void onTeardownRequest(RtspRequestMsg request, Channel channel) {
        // 转发发送给真实的视频源Rtsp服务（IPC）
        sendToMediaServer(request, channel);
    }

    @Override
    public void onPauseRequest(RtspRequestMsg request, Channel channel) {
        // 转发发送给真实的视频源Rtsp服务（IPC）
        sendToMediaServer(request, channel);
    }

    private void sendToMediaServer(RtspRequestMsg request, Channel channel) {
        Attribute<String> sessionAttr = channel.attr(SESSION_ID);
        if (sessionAttr == null) {
            log.error("client:" + channel.remoteAddress() + ", process request error: cannot found session info!");
            channel.writeAndFlush("RTSP/1.0 500 internal error" + Constant.CRLF + Constant.CRLF);
            return;
        }
        String sessionId = sessionAttr.get();
        Session session = SessionManager.getSession(sessionId);
        session.getPlayer().setRequest(request);
        MediaSource mediaSource = session.getMediaSource();
        // 媒体源实体未创建 或者 TODO：媒体源发生变更《ip地址变化》
        if (mediaSource == null ) { // || !request.getIpAddr().equals(mediaSource.getMediaServerIpAddr())
            log.warn("Request Method:{}: no media connection valid! Create new Media server now!", request.getMethod());
            mediaSource = new MediaSource();
            mediaSource.setMediaServerIpAddr(request.getIpAddr());
            mediaSource.setMediaServerPort(request.getPort());
            mediaSource.setSessionId(sessionId);
            mediaSource.setCseq(Integer.parseInt(request.getCSEQ()));
            session.setMediaSource(mediaSource);
        }
        // 发送请求给media server
        mediaSource.sendRequest(request);
    }
}
